# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from datetime import timedelta

from django.utils import timezone

# 无权限或余额不足时的跳转页面
noauth_redirct_url = "/saas/account_main/"
# 无权限或余额不足时的跳转iframe
noauth_redirct_iframe = "/saas/account_noauth/"

# saas系统频道编号
saas_pindao_choice = (("10", "精准营销"), ("20", "贷前审批"), ("30", "贷后监控"), ("40", "宏观战略"), ("50", "地理信息"),)

# saas系统需要计费的项目
saas_item_name = (("10", "征信报告（全景版）"), ("11", "征信报告（标准版）"),
                  ("20", "全维筛查"), ("30", "单点关联（专业版图谱）"), ("31", "单点关联（基础版图谱）"), ("40", "多点关联"), ("50", "族群分析"), ("51", "生命周期预测"),
                  ("60", "预警消息"), ("70", "宏观战略"), ("80", "地理信息"),)

# saas系统计费项目的具体费用
saas_item_fee = {"10": 100, "11": 10, "20": 1, "30": 5, "31": 2, "40": 10, "50": 10, "51": 2, "60": 1, "70": 0, "80": 0.5}

# saas系统计费项目的说明
saas_item_faq = {"10": "单次查看费用，一个月内有效，有效期内计费一次，有效期内查阅不同版本报告收取差价",
                 "11": "单次查看费用，一个月内有效，有效期内计费一次，有效期内查阅不同版本报告收取差价",
                 "20": "筛查的命中结果的每条的费用，有效期一个月",
                 "30": "单次查询费用，有效期一个月",
                 "31": "单次查询费用，有效期一个月",
                 "40": "单次查询费用，有效期一个月",
                 "50": "单次查询费用，有效期一个月",
                 "51": "单次查询费用，有效期一个月",
                 "60": "推送的每个企业每条消息的费用",
                 "70": "单次查询免费，只收取每年的模块使用费",
                 "80": "单次经纬度加载费用", }

# 报告查询有效期，超过此天数重新计费
report_refee_date = 30

# 产业分析的频道代码
statistic_pindao_dict = {"chain": "产业分布", "entmonth": "月度晴雨表", "entage": "企业年龄结构", "tax": "税务分析", "hg": "进出口分析", }


# 校验客户的频道权限、余额或到期日期，并记录费用，否则跳转我的账户页
def customer_verify_fee(request, saas_pindao="", saas_item="", tkey="", count=1):
    quanxian = request.user.u_ucenter.quanxian
    quanxian = quanxian if quanxian else ""
    yue = request.user.u_ucenter.yue
    ctype = request.user.u_ucenter.ctype
    ktdate = request.user.u_ucenter.ktdate
    tnow = timezone.now()

    # 先查看频道权限
    if saas_pindao and saas_pindao not in quanxian:
        return True

    # 检查包时用户的到期时间
    if ctype == "2" and (ktdate - tnow.date()).days <= 0:
        return True
    elif ctype == "1":
        if yue < 100:
            return True
        elif saas_item and saas_item in saas_item_fee.keys():
            from wlviews.saas.models import ELFeeLog

            # 如果是报告，且在缓存时间内，则允许浏览，并根据情况记录差价
            if saas_item.startswith("1") and tkey:
                report_refee_end = tnow - timedelta(report_refee_date)
                count10 = ELFeeLog.objects.filter(customer=request.user, item="10", tkey=tkey, created__gte=report_refee_end).count()
                count11 = ELFeeLog.objects.filter(customer=request.user, item="11", tkey=tkey, created__gte=report_refee_end).count()
                count12 = ELFeeLog.objects.filter(customer=request.user, item="12", tkey=tkey, created__gte=report_refee_end).count()
                if saas_item == "10":
                    if count10:
                        return False
                    elif count11:
                        new_log = {"customer": request.user, "item": saas_item, "count": count, "fee": saas_item_fee["10"] - saas_item_fee["11"],
                                   "ip": request.META.get('REMOTE_ADDR'), "tkey": tkey}
                        ELFeeLog.objects.create(**new_log)
                        return False
                    elif count12:
                        new_log = {"customer": request.user, "item": saas_item, "count": count, "fee": saas_item_fee["10"] - saas_item_fee["12"],
                                   "ip": request.META.get('REMOTE_ADDR'), "tkey": tkey}
                        ELFeeLog.objects.create(**new_log)
                        return False
                elif saas_item == "11":
                    if count10 or count11:
                        return False
                    elif count12:
                        new_log = {"customer": request.user, "item": saas_item, "count": count, "fee": saas_item_fee["11"] - saas_item_fee["12"],
                                   "ip": request.META.get('REMOTE_ADDR'), "tkey": tkey}
                        ELFeeLog.objects.create(**new_log)
                        return False
                elif saas_item == "12":
                    if count10 or count11 or count12:
                        return False

            # 纪录费用
            try:
                count = int(count)
            except ValueError:
                count = 1
            the_fee = saas_item_fee[saas_item] * count
            new_log = {"customer": request.user, "item": saas_item, "count": count, "fee": the_fee, "ip": request.META.get('REMOTE_ADDR')}
            if tkey:
                new_log["tkey"] = tkey
            ELFeeLog.objects.create(**new_log)
    return False
